﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Transfer;
using BranchOfSansar.Register.Reports.Forms;

namespace BranchOfSansar.Register.Trans
{
    public partial class frmTransFix : BranchOfSansar.Register.Trans.frmTransBase
    {
        public frmTransFix()
        {
            InitializeComponent();
        }

        public frmTransFix(long contract_no)
            : this()
        { 
        }

        private void frmTransFix_Load(object sender, EventArgs e)
        {
            cmbType.SelectedIndex = 0;
            txtDate.DateTime = Globals.GetDate();
        }
        private void thinkSum()
        {
            decimal c1 = decimal.Parse(txtBalance.Text); ;
            decimal debitAmount = 0;
            decimal creditAmount = 0;
           // c1 = decimal.Parse(txtLast.Text);

            switch (cmbType.SelectedIndex)
            {
                case 1://Авлага төлөх
                    {
                        creditAmount = txtAmount.Value;
                        break;
                    }
                case 2://Авлага төлөх
                    {
                        creditAmount = txtAmount.Value;
                        break;
                    }
                case 3://Авлага үүсгэх
                    {
                        debitAmount = txtAmount.Value;
                        break;
                    }

                default: break;
            }
            txtLast.Text = (c1 + debitAmount - creditAmount).ToString();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Contract == null || txtAmount.Value==0) return;

            List<Transaction> txn = new List<Transaction>();
            SendContract con = new SendContract()
            {
                ContractNo = Contract.ContractNo,
                Balance = decimal.Parse(txtLast.Text),
                EndDate = Contract.EndDate ?? txtDate.DateTime,
                StartDate = Contract.StartDate ?? txtDate.DateTime,      
            };

            //decimal paid = txtPaidAmount.Value;
            decimal c1 = 0;
            decimal debitAmount = 0;
            decimal creditAmount = 0;
            int status = 1;
            int txnType=0;

            switch (cmbType.SelectedIndex)
            {
                case 0://Butsaan olgolt

                    { 
                        creditAmount = txtAmount.Value;
                        txnType = 1500;
                        break;
                    }
                case 1://Авлага төлөх
                    {
                        c1 = decimal.Parse(txtBalance.Text);
                        creditAmount = txtAmount.Value;
                        txnType = 1203;
                        break;
                    }
                case 2://Авлага хаах хэрэглээгүй үлдэгдэл
                    {
                        c1 = decimal.Parse(txtBalance.Text);
                        creditAmount = txtAmount.Value;
                        txnType = 1600;
                        break;
                    }
                case 3://Авлага үүсгэх
                    {
                        c1 = decimal.Parse(txtBalance.Text);
                        debitAmount = txtAmount.Value;
                        txnType = 1400;
                        break;
                    }
                default: break;
            }

            if (txtAmount.Value > 0)
            {
                txn.Add(new Transaction()
                {
                    ContractNo = Contract.ContractNo,
                    //ProdCode = p.ProdCode,
                    Quantity=0,
                    Status = 1,
                    StartDate = txtDate.DateTime,
                    EndDate = null,
                    TxnCategory = ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbTransCategory.SelectedItem).StrValue,
                    BankNo = cmbBank.Enabled ? ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbBank.SelectedItem).IntValue : 0,
                    TxnPrice = 0,
                    TxnFirstBalance = c1,
                    TxnDebtAmount = debitAmount,
                    TxnAmount=creditAmount,
                    TxnDate = txtDate.DateTime,
                    TxnDesc = cmbType.Text+": "+ txtDesc.Text,
                    TxnType = txnType,
                    UserNo = Globals.UserId
                });
              }
            

            using (TransferClient tc = new TransferClient(Globals.Token))
            {
                var res = tc.DoTransaction(txn.ToArray(), new SendTransfer[] { }, con);
                if (res.Code != 0)
                    Globals.Warr(res.Message);
                else
                {
                    Globals.Info(res.Message);
                    this.JrNo = long.Parse(res.RetData.ToString());
                    if (PrintReceipt() == System.Windows.Forms.DialogResult.Cancel)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                }
            }
        }

        private void labelControl13_Click(object sender, EventArgs e)
        {

        }

        private void frmTransFix_ContractSelected()
        {
            thinkSum();
        }

        private void txtAmount_EditValueChanged(object sender, EventArgs e)
        {
            thinkSum();
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
